
$(function () {
    $("#emifile_upload").uploadify({
        'auto': false,
        'multi': false,
        'buttonText': 'Upload Leads',
        'buttonClass': 'btn btn-upload-leads',
        'width': 107,
        'height': 35,
        'fileTypeDesc': 'Excel Sheet',
        'fileTypeExts': '*.xls; *.xlsx; *.csv',
        'fileSizeLimit': '4194304B',
        'swf': '/Images/uploadify.swf',
        'scriptData': { 'token': tokenValue },
        'uploader': '/EnhancedManualImport/Upload/?token=' + tokenValue,
        'successTimeout': 200,
        'onCancel': function (file) {

        },
        'onSelect': function (file) {

            $('#StatusMessage').text("");
            $('#StatusMessage').append("Select <strong>Continue</strong> to go to Step 2.  (If you would like to upload a different file, you can delete this file and upload a new one.)");

            $('#Ms_successful').text("");
            $('#Ms_successful').css("display", "inline-block");
            $('#emifile_upload').uploadify('disable', true);
            $('#Ms_successful').append("<span id='wrappfilename'><strong>File Name: </strong>" + file.name +"</span>" + "<a id='DeleteFile' href=\"javascript:DeleteUpload();\" class='btn btn-x'>X</a>");
            /*to be disable click on "upload leads" button*/
            $('#emifile_upload-button').removeClass('btn-upload-leads');
            $('#emifile_upload-button').addClass('btn-upload-leads-gray');

            var h = $('#wrappfilename').height();
            if (h > 15) {
                $('#DeleteFile').css("top", -10);
                $('#DeleteFile').css("left", 15);
                $('#Ms_successful').css("padding-right", 20);

            }

            /*to be able click on "Continue" button*/
            $('#BtnContinue').removeClass('btn-continue-gray');
            $('#BtnContinue').addClass('btn-continue-blue');
            $('#BtnContinue').attr("onclick", "javascript:UploadFile();");
        },
        'onUploadSuccess': function (file, data, response) {
            $.fancybox.close();
            var jsonData = jQuery.parseJSON(data);
            if (jsonData.IsError) {
                showCustomError(jsonData.ErrorMessage);

                // TODO: May also replace this!
                getLeadModel.DisplayFirstView(true);
                getLeadModel.DisplayHelp(false);
                getLeadModel.DisplaySecondView(false);
                getLeadModel.DisplayThirdView(false);
                DeleteUpload();
            }
            else {
                var ManualImportId = jsonData.ManualImportId;
                $('#StoreManualID').val(ManualImportId);
                var SessionId = jsonData.SessionId;
                $('#StoreSession').val(SessionId);

                getLeadModel.DisplayFirstView(false);
                getLeadModel.DisplayHelp(true);
                getLeadModel.DisplaySecondView(false);
                getLeadModel.DisplayThirdView(false);
            }
        },
        'onUploadError': function (file, errorCode, errorMsg, errorString) {
            $.fancybox.close();

            getLeadModel.DisplayFirstView(true);
            getLeadModel.DisplayHelp(false);
            getLeadModel.DisplaySecondView(false);
            getLeadModel.DisplayThirdView(false);

            if (errorCode && $.trim(errorCode.toString()) != "-280") {
                showCustomError('The file ' + file.name + ' could not be uploaded: ErrorCode' + errorCode + ' Error String: ' + errorString);
            }
            DeleteUpload();
        },
        'onSelectError': function (file, errorCode, errorMsg) {
            if (errorCode == FILE_EXCEEDS_SIZE_LIMIT) {
                $('#Ms_successful').text("");
                $('#Ms_successful').css("display", "inline-block");
                $('#Ms_successful').append("File " + file.name + " exceeds the maximum file size - 4MB");
            }
            else {
                showCustomError('The file ' + file.name + ' returned an error and was not added to the queue. Error message:' + errorMsg);
            }
        }

    });

});

function DeleteUpload() {
	$('#StatusMessage').text("To import leads, first upload your list of leads (please see the Help information to the right).  We'll then guide you through the process of matching your column headers to ours, and also notify you of any errors we've found in the file.");
	$('#emifile_upload').uploadify('cancel');
	$('#emifile_upload').uploadify('disable', false);
	$('#Ms_successful').text("");
	$('#emifile_upload-button').removeClass('btn-upload-leads-gray');
	$('#emifile_upload-button').addClass('btn-upload-leads');

	$('#Ms_successful').css("display", "none");
	$('#BtnContinue').removeClass('btn-continue-blue');
	$('#BtnContinue').addClass('btn-continue-gray');
	$('#BtnContinue').attr("onclick", "");
}

function UploadFile() {
    $(".fancybox-skin").show();
    $("#fancybox-overlay").show();
    $("#fancybox-overlay").css("opacity", "0.8");

    $.fancybox($('#popup-processing-importleads'), {
        padding: 0,
        closeBtn: false,
        helpers: {
            overlay: {
                opacity: 0.8,
                css: { 'background-color': '#11619d' },
                closeClick: false
            }
        }
    });
    $('#emifile_upload').uploadify('upload');
}
